/*
 * Description: 字典状态中心
 * Company: 联宝（合肥）电子科技有限公司
 * Author: Dongxu.Ni
 * Date: 2020/3/10 15:20
 */
import { dictList } from '../../api/sys/dict'
import { getLangMappingProperty } from '../../lang/lang-set'

const state = {
  // 自定义常用字典
  customDictCollects: [
    // 是否
    { code: 'YN', valueEn: 'YN', valueZhCn: 'YN', valueZhTw: 'YN', parentCode: 'root', order: 0 },
    { code: true, valueEn: 'yes', valueZhCn: '是', valueZhTw: '是', parentCode: 'YN', order: 0 },
    { code: false, valueEn: 'no', valueZhCn: '否', valueZhTw: '否', parentCode: 'YN', order: 1 },
    // 是否
    {
      code: 'YnNum',
      valueEn: 'YnNum',
      valueZhCn: 'YnNum',
      valueZhTw: 'YnNum',
      parentCode: 'root',
      order: 0
    },
    { code: 1, valueEn: 'yes', valueZhCn: '是', valueZhTw: '是', parentCode: 'YnNum', order: 0 },
    { code: 0, valueEn: 'no', valueZhCn: '否', valueZhTw: '否', parentCode: 'YnNum', order: 1 },
    // 按钮
    {
      code: 'BUTTON',
      valueEn: 'BUTTON',
      valueZhCn: '按钮',
      valueZhTw: '按钮',
      parentCode: 'root',
      order: 1
    },
    {
      code: 'init',
      valueEn: 'init',
      valueZhCn: '初始化',
      valueZhTw: '初始化',
      parentCode: 'BUTTON',
      order: 0
    },
    {
      code: 'query',
      valueEn: 'query',
      valueZhCn: '查询',
      valueZhTw: '查询',
      parentCode: 'BUTTON',
      order: 1
    },
    {
      code: 'add',
      valueEn: 'add',
      valueZhCn: '新增',
      valueZhTw: '新增',
      parentCode: 'BUTTON',
      order: 2
    },
    {
      code: 'edit',
      valueEn: 'edit',
      valueZhCn: '编辑',
      valueZhTw: '编辑',
      parentCode: 'BUTTON',
      order: 3
    },
    {
      code: 'view',
      valueEn: 'view',
      valueZhCn: '查看',
      valueZhTw: '查看',
      parentCode: 'BUTTON',
      order: 4
    },
    {
      code: 'viewDetail',
      valueEn: 'view detail',
      valueZhCn: '查看详细',
      valueZhTw: '查看详细',
      parentCode: 'BUTTON',
      order: 4
    },
    {
      code: 'delete',
      valueEn: 'delete',
      valueZhCn: '删除',
      valueZhTw: '删除',
      parentCode: 'BUTTON',
      order: 5
    },
    {
      code: 'save',
      valueEn: 'save',
      valueZhCn: '保存',
      valueZhTw: '保存',
      parentCode: 'BUTTON',
      order: 6
    },
    {
      code: 'import',
      valueEn: 'import',
      valueZhCn: '导入',
      valueZhTw: '导入',
      parentCode: 'BUTTON',
      order: 7
    },
    {
      code: 'export',
      valueEn: 'export',
      valueZhCn: '导出',
      valueZhTw: '导出',
      parentCode: 'BUTTON',
      order: 8
    },
    {
      code: 'exportCurrent',
      valueEn: 'exportCurrent',
      valueZhCn: '导出当前',
      valueZhTw: '导出当前',
      parentCode: 'BUTTON',
      order: 9
    },
    {
      code: 'exportAll',
      valueEn: 'exportAll',
      valueZhCn: '导出全部',
      valueZhTw: '导出全部',
      parentCode: 'BUTTON',
      order: 10
    },
    {
      code: 'checkUpdate',
      valueEn: 'checkUpdate',
      valueZhCn: '检查更新',
      valueZhTw: '检查更新',
      parentCode: 'BUTTON',
      order: 11
    },
    {
      code: 'assign',
      valueEn: 'assign',
      valueZhCn: '分配',
      valueZhTw: '分配',
      parentCode: 'BUTTON',
      order: 12
    },
    {
      code: 'reAssign',
      valueEn: 'reAssign',
      valueZhCn: '重配',
      valueZhTw: '重配',
      parentCode: 'BUTTON',
      order: 12
    },
    {
      code: 'reset',
      valueEn: 'reset',
      valueZhCn: '重置',
      valueZhTw: '重置',
      parentCode: 'BUTTON',
      order: 13
    },
    {
      code: 'addToLocal',
      valueEn: 'add to local',
      valueZhCn: '添加到本地',
      valueZhTw: '添加到本地',
      parentCode: 'BUTTON',
      order: 14
    },
    {
      code: 'receive',
      valueEn: 'receive',
      valueZhCn: '接收',
      valueZhTw: '接收',
      parentCode: 'BUTTON',
      order: 15
    },
    {
      code: 'reject',
      valueEn: 'reject',
      valueZhCn: '驳回',
      valueZhTw: '驳回',
      parentCode: 'BUTTON',
      order: 16
    },
    {
      code: 'clear',
      valueEn: 'clear',
      valueZhCn: '清空',
      valueZhTw: '清空',
      parentCode: 'BUTTON',
      order: 17
    },
    {
      code: 'exportByCommodity',
      valueEn: 'export by commodity',
      valueZhCn: '导出(Commodity维度)',
      valueZhTw: '导出(Commodity维度)',
      parentCode: 'BUTTON',
      order: 18
    },
    {
      code: 'exportFileByGcmOwner',
      valueEn: 'export file by gcmOwner',
      valueZhCn: '导出(GcmOwner维度)',
      valueZhTw: '导出(GcmOwner维度)',
      parentCode: 'BUTTON',
      order: 19
    },
    {
      code: 'syncData',
      valueEn: 'sync data',
      valueZhCn: '手动同步',
      valueZhTw: '手动同步',
      parentCode: 'BUTTON',
      order: 20
    },
    {
      code: 'refreshPrice',
      valueEn: 'refresh price',
      valueZhCn: '刷新价格',
      valueZhTw: '刷新价格',
      parentCode: 'BUTTON',
      order: 21
    },
    {
      code: 'appoint',
      valueEn: 'appoint',
      valueZhCn: '指派',
      valueZhTw: '指派',
      parentCode: 'BUTTON',
      order: 21
    },
    {
      code: 'receiveAgain',
      valueEn: 'receive again',
      valueZhCn: '重新接收',
      valueZhTw: '重新接收',
      parentCode: 'BUTTON',
      order: 22
    },
    {
      code: 'returnTo',
      valueEn: 'returnTo',
      valueZhCn: '退回',
      valueZhTw: '退回',
      parentCode: 'BUTTON',
      order: 23
    },
    {
      code: 'evaluationAssign',
      valueEn: 'evaluation assign',
      valueZhCn: '估价分配',
      valueZhTw: '估价分配',
      parentCode: 'BUTTON',
      order: 24
    },
    {
      code: 'evaluationApprove',
      valueEn: 'evaluation approve',
      valueZhCn: '估价审批',
      valueZhTw: '估价审批',
      parentCode: 'BUTTON',
      order: 25
    },
    {
      code: 'evaluation',
      valueEn: 'evaluation',
      valueZhCn: '估价',
      valueZhTw: '估价',
      parentCode: 'BUTTON',
      order: 26
    },
    {
      code: 'quote',
      valueEn: 'quote',
      valueZhCn: '报价',
      valueZhTw: '报价',
      parentCode: 'BUTTON',
      order: 27
    },
    // 优先级
    {
      code: 'IssueLevel',
      valueEn: 'IssueLevel',
      valueZhCn: 'IssueLevel',
      valueZhTw: 'IssueLevel',
      parentCode: 'root',
      order: 0
    },
    {
      code: 0,
      valueEn: 'Best Level',
      valueZhCn: '最优先',
      valueZhTw: '最优先',
      parentCode: 'IssueLevel',
      icon: require('@/assets/icons/thunder.png'),
      order: 0
    },
    {
      code: 1,
      valueEn: 'Medium Level',
      valueZhCn: '中等',
      valueZhTw: '中等',
      parentCode: 'IssueLevel',
      icon: require('@/assets/icons/run.png'),
      order: 1
    },
    {
      code: 2,
      valueEn: 'Lowest Level',
      valueZhCn: '最低',
      valueZhTw: '最低',
      parentCode: 'IssueLevel',
      icon: require('@/assets/icons/coffe.png'),
      order: 2
    },
    // 问题类型
    {
      code: 'IssueType',
      valueEn: 'IssueType',
      valueZhCn: 'IssueType',
      valueZhTw: 'IssueType',
      parentCode: 'root',
      order: 0
    },
    {
      code: 0,
      valueEn: 'Bug',
      valueZhCn: 'Bug',
      valueZhTw: 'Bug',
      parentCode: 'IssueType',
      icon: require('@/assets/icons/bug.png'),
      order: 0
    },
    {
      code: 1,
      valueEn: 'Requirement',
      valueZhCn: '需求',
      valueZhTw: '需求',
      parentCode: 'IssueType',
      icon: require('@/assets/icons/require.png'),
      order: 1
    },
    {
      code: 2,
      valueEn: 'Other',
      valueZhCn: '其他',
      valueZhTw: '其他',
      parentCode: 'IssueType',
      icon: require('@/assets/icons/other.png'),
      order: 2
    },
    // 问题状态
    {
      code: 'IssueStatus',
      valueEn: 'IssueStatus',
      valueZhCn: 'IssueStatus',
      valueZhTw: 'IssueStatus',
      parentCode: 'root',
      order: 0
    },
    {
      code: 0,
      valueEn: 'willresolve',
      valueZhCn: '待解决',
      valueZhTw: '待解决',
      parentCode: 'IssueStatus',
      icon: require('@/assets/icons/time.png'),
      order: 0
    },
    {
      code: 1,
      valueEn: 'resolving',
      valueZhCn: '解决中',
      valueZhTw: '解决中',
      parentCode: 'IssueStatus',
      icon: require('@/assets/icons/loading.png'),
      order: 1
    },
    {
      code: 2,
      valueEn: 'resolved',
      valueZhCn: '已解决',
      valueZhTw: '已解决',
      parentCode: 'IssueStatus',
      icon: require('@/assets/icons/good.png'),
      order: 2
    },
    {
      code: 3,
      valueEn: 'unsolved',
      valueZhCn: '未解决',
      valueZhTw: '未解决',
      parentCode: 'IssueStatus',
      icon: require('@/assets/icons/stop.png'),
      order: 3
    },
    {
      code: 4,
      valueEn: 'complete',
      valueZhCn: '已关闭',
      valueZhTw: '已关闭',
      parentCode: 'IssueStatus',
      icon: require('@/assets/icons/close.png'),
      order: 4
    },
    // 模型配置
    {
      code: 'ModelConfig',
      valueEn: 'ModelConfig',
      valueZhCn: 'ModelConfig',
      valueZhTw: 'ModelConfig',
      parentCode: 'root',
      order: 0
    },
    {
      code: 0,
      valueEn: 'plastic pellets',
      valueZhCn: '塑胶粒',
      valueZhTw: '塑胶粒',
      parentCode: 'ModelConfig',
      type: 1, // 是否是复杂配置 0 不是 1 是
      configKey: 'plastic_pellets',
      gcmFunction: 'me',
      order: 0
    },
    {
      code: 1,
      valueEn: 'machine tonnage',
      valueZhCn: '机台吨位',
      valueZhTw: '机台吨位',
      parentCode: 'ModelConfig',
      type: 1,
      configKey: 'machine_tonnage',
      gcmFunction: 'me',
      order: 1
    },
    {
      code: 2,
      valueEn: 'labor rate',
      valueZhCn: '人工费率',
      valueZhTw: '人工费率',
      parentCode: 'ModelConfig',
      type: 0,
      configKey: 'labor_rate',
      gcmFunction: 'me',
      order: 2
    },
    {
      code: 3,
      valueEn: 'hot melt rate',
      valueZhCn: '热熔费率',
      valueZhTw: '热熔费率',
      parentCode: 'ModelConfig',
      type: 0,
      configKey: 'hot_melt_rate',
      gcmFunction: 'me',
      order: 3
    },
    {
      code: 4,
      valueEn: 'management profit',
      valueZhCn: '营销/利润',
      valueZhTw: '营销/利润',
      parentCode: 'ModelConfig',
      type: 0,
      configKey: 'management_profit',
      gcmFunction: 'me',
      order: 4
    },
    // 币种
    {
      code: 'Currency',
      valueEn: 'Currency',
      valueZhCn: 'Currency',
      valueZhTw: 'Currency',
      parentCode: 'root',
      order: 0
    },
    {
      code: 0,
      valueEn: 'USD',
      valueZhCn: 'USD',
      valueZhTw: 'USD',
      parentCode: 'Currency',
      order: 0
    },
    {
      code: 1,
      valueEn: 'RMB',
      valueZhCn: 'RMB',
      valueZhTw: 'RMB',
      parentCode: 'Currency',
      order: 1
    },
    // Solution状态

    {
      code: 'SolutionStatus',
      valueEn: 'SolutionStatus',
      valueZhCn: 'SolutionStatus',
      valueZhTw: 'SolutionStatus',
      parentCode: 'root',
      order: 0
    },
    {
      code: 10,
      valueEn: 'created',
      valueZhCn: '已创建',
      valueZhTw: '已创建',
      parentCode: 'SolutionStatus',
      order: 0
    },
    {
      code: 20,
      valueEn: 'checked',
      valueZhCn: '已检查',
      valueZhTw: '已检查',
      parentCode: 'SolutionStatus',
      order: 1
    },
    {
      code: 30,
      valueEn: 'sent',
      valueZhCn: '已发送',
      valueZhTw: '已发送',
      parentCode: 'SolutionStatus',
      order: 2
    },
    {
      code: 40,
      valueEn: 'completed',
      valueZhCn: '已完成',
      valueZhTw: '已完成',
      parentCode: 'SolutionStatus',
      order: 3
    }
  ],
  // 全局字典集合数据
  dictCollects: []
}

const getters = {
  dictOptions: state => code => {
    return state.dictCollects.filter(dict => dict.parentCode === code)
  },
  dictValue: state => code => {
    const dict = state.dictCollects.find(dict => dict.code === code)
    return dict ? dict.value : code
  },
  dictValueObj: state => code => {
    const dict = state.dictCollects.find(dict => dict.code === code)
    return dict ? dict.value : null
  },
  dictValueByParent: state => (parentCode, code) => {
    const list = state.dictCollects.filter(dict => dict.parentCode === parentCode)
    const dict = list.find(dict => dict.code === code)
    return dict ? dict.value : code
  },
  dictIconByParent: state => (parentCode, code) => {
    const list = state.dictCollects.filter(dict => dict.parentCode === parentCode)
    const dict = list.find(dict => dict.code === code)
    return dict ? dict.icon : ''
  }
}

const mutations = {
  SET_DICT: (state, dictCollects) => {
    state.dictCollects = dictCollects
  },
  REMOVE_DICT: state => {
    state.dictCollects = []
  },
  SET_DICT_WITH_LANGUAGE: (state, language) => {
    const langMappingProperty = getLangMappingProperty(language, 'value')
    for (let i = 0, len = state.dictCollects.length; i < len; i++) {
      state.dictCollects[i].value = state.dictCollects[i][langMappingProperty]
    }
  }
}

const actions = {
  // 初始化字典信息
  initDictCollects({ commit, state, rootState }) {
    const language = rootState.language.language
    const langMappingProperty = getLangMappingProperty(language, 'value')
    return new Promise((resolve, reject) => {
      dictList()
        .then(response => {
          const dictList = state.customDictCollects.concat(response.data)
          // 排序
          dictList.sort((a, b) => {
            return a.order - b.order
          })
          for (let i = 0, len = dictList.length; i < len; i++) {
            dictList[i].value = dictList[i][langMappingProperty]
          }
          commit('SET_DICT', dictList)
          resolve()
        })
        .catch(err => reject(err))
    })
  }
}

export default {
  namespaced: true,
  state,
  getters,
  mutations,
  actions
}
